package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.UserPostBean;
import cst.neu.yuejian_31.iface.PostCountListener;
import cst.neu.yuejian_31.iface.UserPostIface;
import cst.neu.yuejian_31.iface.UserPostListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.UserPostService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/23.
 */

public class UserPostModel implements UserPostIface {
    private Retrofit retrofit;
    private int count;
    public UserPostModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getUserPost(String userId, final UserPostListener userPostListener) {
        UserPostService userPostService=retrofit.create(UserPostService.class);
        Call<UserPostBean>call=
                userPostService.getUserPost(userId);
        call.enqueue(new Callback<UserPostBean>() {
            @Override
            public void onResponse(Call<UserPostBean> call, Response<UserPostBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    userPostListener.onSuccess(response.body().getData());
                    count=response.body().getCount();
                }else {
                    userPostListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<UserPostBean> call, Throwable t) {
                userPostListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getPostCount(String userId, final PostCountListener postCountListener) {
        UserPostService userPostService=retrofit.create(UserPostService.class);
        Call<UserPostBean>call=
                userPostService.getUserPost(userId);
        call.enqueue(new Callback<UserPostBean>() {
            @Override
            public void onResponse(Call<UserPostBean> call, Response<UserPostBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    postCountListener.onSuccess(response.body());
                }else {
                    postCountListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<UserPostBean> call, Throwable t) {
                postCountListener.onFail(t.toString());
            }
        });
    }
}
